// 加载 face-api.js
importScripts(chrome.runtime.getURL('lib/face-api.min.js'));

let modelLoaded = false;

// 监听消息
self.onmessage = async function(e) {
  try {
    if (!modelLoaded) {
      await faceapi.nets.ssdMobilenetv1.loadFromUri(chrome.runtime.getURL('models'));
      modelLoaded = true;
    }

    const { imageUrl, imageId } = e.data;
    const img = await faceapi.fetchImage(imageUrl);
    const detections = await faceapi.detectAllFaces(img);

    self.postMessage({
      success: true,
      imageId,
      hasOneFace: detections.length === 1
    });
  } catch (error) {
    self.postMessage({
      success: false,
      imageId: e.data.imageId,
      error: error.message
    });
  }
}; 